﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using MailKit.Net.Smtp;
using MimeKit;
using MimeKit.Text;
using Entity;
using IRealize;
using Realize;

namespace WebApplication9.Controllers.Recruit_Manage.LY
{
    public class ER_LYSPController : Controller
    {
        private readonly IConfiguration configuration;

        public ER_LYSPController(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        public IActionResult Index()
        {
            return View();
        }

        public async Task<IActionResult> Select_LY(int CurrentPage, int PageSize)
        {
            IER eR = new ER();
            List<Engage_Resume> zhi = await eR.Select_LySp(configuration["SqlServer"]);
            List<Engage_Resume> zhi2 = zhi.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList();
            PagedViewModel<Engage_Resume> cc = new PagedViewModel<Engage_Resume>() { itemas = zhi2, total = zhi.Count };
            return Ok(new
            {
                list = cc
            });
        }

        public IActionResult Upd(string id)
        {
            ViewBag.TitleId = id;
            return View();
        }

        // 网易邮箱授权码CTPJ934N677R243C
        public async Task<IActionResult> Upd_ER(string id, string zhi,string yx)
        {
            IER eR = new ER();
            int result = await eR.Lysp(id, zhi, configuration["SqlServer"]);

            if (result > 0)
            {
                // 获取候选人信息
                //var candidate = await eR.GetCandidateInfo(id, configuration["SqlServer"]);

                // 发送邮件通知
                try
                {
                    await SendEmailNotification(yx, zhi);
                }
                catch (Exception ex)
                {
                    // 邮件发送失败，但不影响主业务流程
                    Console.WriteLine($"邮件发送失败: {ex.Message}");
                }

                return Ok(new
                {
                    zhi = "ok",
                });
            }
            else
            {
                return Ok(new
                {
                    zhi = "nook"
                });
            }
        }

        // 发送邮件通知方法
        private async Task SendEmailNotification(string recipientEmail, string approvalStatus)
        {
            // 创建邮件消息
            var email = new MimeMessage();
            email.From.Add(MailboxAddress.Parse("15111297295@163.com"));
            email.To.Add(MailboxAddress.Parse(recipientEmail));
            email.Subject = "录用申请审批结果通知";

            // 设置邮件正文
            string body = $"您好，您的录用申请已完成审批，审批结果为：{approvalStatus}。感谢您的申请！";
            email.Body = new TextPart(TextFormat.Plain) { Text = body };

            // 配置 SMTP 客户端并发送邮件
            using var smtp = new SmtpClient();
            await smtp.ConnectAsync("smtp.163.com", 465, true);
            await smtp.AuthenticateAsync("15111297295@163.com", "CTPJ934N677R243C");
            await smtp.SendAsync(email);
            await smtp.DisconnectAsync(true);
        }
    }
}